//
//  GreyOrViewController.m
//  HuiKaoClient
//
//  Created by 龚 杰 on 13-2-20.
//  Copyright (c) 2013年 龚 杰. All rights reserved.
//

#import "GreyOrViewController.h"
#import "MainTabBarController.h"
#import "GOWebViewController.h"
#import "AppDelegate.h"
#import <QuartzCore/QuartzCore.h>
@interface GreyOrViewController ()

@end

@implementation GreyOrViewController
- (id)init
{
    if (self = [super init]) {
        m_Goindextype = GREYOR_RS;
        m_DataArraylist1 = [[NSMutableArray alloc] init];
        m_DataArraylist2 = [[NSMutableArray alloc] init];
        m_DataArraylist3 = [[NSMutableArray alloc] init];
    }
    return self;
}
-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([m_titleBttBarItem superview] == nil) {
        [self.navigationController.navigationBar addSubview:m_titleBttBarItem];
    }
    MainTabBarController* tMainTabController = (MainTabBarController*)self.navigationController.tabBarController;
    tMainTabController.mainselectedIndex =  tMainTabController.selectedIndex;
    
    


}

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if ([m_titleBttBarItem superview]) {
        [m_titleBttBarItem removeFromSuperview];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        if ([self.navigationController.navigationBar viewWithTag:0x300] == nil) {
            UIImageView* tImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titleBar.png"]];
            tImgView.tag =0x300;
            tImgView.frame =CGRectMake(0, 0, 320, 44);
            [self.navigationController.navigationBar addSubview:tImgView];
            [tImgView release];
        }
    }
    m_titleBttBarItem = [[MainTabBarController setTitle:@"报名查分"] retain];
    [self CreateHeadView];
    m_GoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 75,self.view.frame.size.width,self.view.frame.size.height-75-44-49) style:UITableViewStylePlain];
    [m_GoTableView setBackgroundColor:[UIColor clearColor]];
    [m_GoTableView setSeparatorStyle:NO];
    UIView* iview = [[UIView alloc] initWithFrame:m_GoTableView.frame];
    [iview setBackgroundColor:BACKGROUND_COLOR];
    
    m_GoTableView.backgroundView = iview;
    [iview release];
    [m_GoTableView setDataSource:self];
    [m_GoTableView setDelegate:self];
    [self.view addSubview:m_GoTableView];
    
    HttpInterfaceManager* iHttpInterface = [HttpInterfaceManager sharedManager];
    iHttpInterface.delegate = self;
    [iHttpInterface GetGOSURequestGet];
    [self addHudLoad];
}

- (void)CreateHeadView
{
    UIView* ihead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 75)];
    ihead.backgroundColor = [UIColor clearColor];

    
    UIView* ihead_tab = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 35)];
    ihead_tab.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"subtitlebg.png"]];
    
    [ihead addSubview:ihead_tab];
    [ihead_tab release];
    
    NSInteger w = self.view.frame.size.width/3;
    m_RSbutton = [UIButton buttonWithType:UIButtonTypeCustom];
     m_RSbutton.frame = CGRectMake(0, 0,w, 34);
    m_RSbutton.tag = 1002;
    [m_RSbutton setTitleColor:[UIColor colorWithRed:121.0/255.0f green:121.0/255.0f blue:121.0/255.0f alpha:1.0f]
                     forState:UIControlStateNormal];
    [m_RSbutton setTitle:@"报名" forState:UIControlStateNormal];
    m_RSbutton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [m_RSbutton addTarget:self action:@selector(ChangeButton:) forControlEvents:UIControlEventTouchUpInside];
    [ihead addSubview:m_RSbutton];
    
    m_Tbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    m_Tbutton.frame = CGRectMake(w,  0,w , 34);
    m_Tbutton.tag = 1003;
    [m_Tbutton setTitleColor:[UIColor colorWithRed:121.0/255.0f green:121.0/255.0f blue:121.0/255.0f alpha:1.0f]
                    forState:UIControlStateNormal];
    [m_Tbutton setTitle:@"考试" forState:UIControlStateNormal];
    m_Tbutton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [m_Tbutton addTarget:self action:@selector(ChangeButton:) forControlEvents:UIControlEventTouchUpInside];
    [ihead addSubview:m_Tbutton];
    
    m_GObutton = [UIButton buttonWithType:UIButtonTypeCustom];


    m_GObutton.tag = 1004;
    m_GObutton.frame = CGRectMake(w*2,0, w+2, 34);
    m_GObutton.titleLabel.textColor = [UIColor blackColor];
    [m_GObutton setTitleColor:[UIColor colorWithRed:121.0/255.0f green:121.0/255.0f blue:121.0/255.0f alpha:1.0f]
                     forState:UIControlStateNormal];
    [m_GObutton setTitle:@"查分" forState:UIControlStateNormal];
    m_GObutton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [m_GObutton addTarget:self action:@selector(ChangeButton:) forControlEvents:UIControlEventTouchUpInside];
    [ihead addSubview:m_GObutton];
  
    
    m_label = [[UILabel alloc] initWithFrame:CGRectMake(1, 35,w, 40)];
    [m_label setFont:[UIFont systemFontOfSize:15.0f]];
    [m_label setTextColor:[UIColor colorWithRed:19.0/255.0f green:19.0/255.0f blue:19.0/255.0f alpha:1.0f]];
    m_label.backgroundColor = BACKGROUND_COLOR;
    m_label.textAlignment = UITextAlignmentCenter;
    m_label.text = @"各省";
    [ihead addSubview:m_label];
    
    m_label1 = [[UILabel alloc] initWithFrame:CGRectMake(1+w, 35,w, 40)];
    [m_label1 setFont:[UIFont systemFontOfSize:15.0f]];
    [m_label1 setTextColor:[UIColor colorWithRed:19.0/255.0f green:19.0/255.0f blue:19.0/255.0f alpha:1.0f]];
    m_label1.backgroundColor = BACKGROUND_COLOR;
    m_label1.textAlignment = UITextAlignmentCenter;
    [ihead addSubview:m_label1];
    
    m_label2 = [[UILabel alloc] initWithFrame:CGRectMake(1+w*2, 35,w, 40)];
    [m_label2 setFont:[UIFont systemFontOfSize:15.0f]];
    [m_label2 setTextColor:[UIColor colorWithRed:19.0/255.0f green:19.0/255.0f blue:19.0/255.0f alpha:1.0f]];
    m_label2.backgroundColor = BACKGROUND_COLOR;
    m_label2.textAlignment = UITextAlignmentCenter;
    [ihead addSubview:m_label2];
    
    UIImageView* tLineImgView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line.png"]];
    tLineImgView.frame =CGRectMake(0, 74, 320, 1);
    [ihead addSubview:tLineImgView];
    [tLineImgView release];
    
    [self.view addSubview:ihead];
    [ihead release];
    
    switch (m_Goindextype) {
        case GREYOR_RS:
        {
            [m_RSbutton setBackgroundColor:[UIColor whiteColor]];
            m_label1.text = @"报名时间";
            m_label2.text = @"报名入口";
            break;
        }
        case GREYOR_T:
        {
            [m_Tbutton setBackgroundColor:[UIColor whiteColor]];
            m_label1.text = @"考试时间";
            m_label2.text = @"专题";
            break;
        }
        case GREYOR_GO:
        {
            [m_GObutton setBackgroundColor:[UIColor whiteColor]];
            m_label1.text = @"查分时间";
            m_label2.text = @"查分入口";
            break;
        }
    }
}
- (void)ChangeButton:(id)button
{
    UIButton* ibutton = button;
    switch (ibutton.tag) {
        case 1002:
        {
            m_Goindextype = GREYOR_RS;
            [m_RSbutton setBackgroundColor:[UIColor whiteColor]];
            [m_Tbutton setBackgroundColor:[UIColor clearColor]];
            [m_GObutton setBackgroundColor:[UIColor clearColor]];
            m_label1.text = @"报名时间";
            m_label2.text = @"报名入口";
            [m_GoTableView reloadData];
            break;
        }
        case 1003:
        {
            m_Goindextype = GREYOR_T;
            [m_RSbutton setBackgroundColor:[UIColor clearColor]];
            [m_Tbutton setBackgroundColor:[UIColor whiteColor]];
            [m_GObutton setBackgroundColor:[UIColor clearColor]];
            m_label1.text = @"考试时间";
            m_label2.text = @"专题";
            [m_GoTableView reloadData];
            break;
        }
        case 1004:
        {
            m_Goindextype = GREYOR_GO;
            [m_RSbutton setBackgroundColor:[UIColor clearColor]];
            [m_Tbutton setBackgroundColor:[UIColor clearColor]];
            [m_GObutton setBackgroundColor:[UIColor whiteColor]];
            m_label1.text = @"查分时间";
            m_label2.text = @"查分入口";
            [m_GoTableView reloadData];
            break;
        }
        default:
            break;
    }
}
- (void)ChangeCellButton:(id)button
{
    if (button == nil) {
        return;
    }
    UIButton* ibutton = (UIButton*)button;
    NSDictionary* dic = nil;
    if (m_Goindextype == GREYOR_RS) {
        dic = [m_DataArraylist1 objectAtIndex:ibutton.tag];
    }
    else if (m_Goindextype == GREYOR_T) {
        dic = [m_DataArraylist2 objectAtIndex:ibutton.tag];
        
    }
    else if (m_Goindextype == GREYOR_GO) {
        dic = [m_DataArraylist3 objectAtIndex:ibutton.tag];
    }
    GOWebViewController* tWebNewsVC = [[GOWebViewController alloc] init];
    tWebNewsVC.WebURL =(NSString*)[dic objectForKey:@"url"];
    NSLog(@"%@",tWebNewsVC.WebURL);
    tWebNewsVC.hidesBottomBarWhenPushed =YES;
    tWebNewsVC.navigationItem.hidesBackButton =YES;
    [self.navigationController pushViewController:tWebNewsVC animated:YES];
    [tWebNewsVC release];
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (m_Goindextype == GREYOR_RS) {
        return  [m_DataArraylist1 count];
    }
    else if (m_Goindextype == GREYOR_T) {
         return  [m_DataArraylist2 count];
    }
    else if (m_Goindextype == GREYOR_GO) {
         return  [m_DataArraylist3 count];
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* tCellstr = @"EXRCell";
    UITableViewCell* tCell = [tableView dequeueReusableCellWithIdentifier:tCellstr];
    if (tCell == nil) {
        tCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tCellstr] autorelease];
//        tCell.accessoryType = UITableViewCellAccessoryNone;
        tCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel* iName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,106, 40)];
        iName.tag = 100;
        iName.backgroundColor = [UIColor clearColor];
        [iName setFont:[UIFont systemFontOfSize:15.0f]];
        [iName setTextColor:[UIColor colorWithRed:19.0/255.0f green:19.0/255.0f blue:19.0/255.0f alpha:1.0f]];
        iName.textAlignment = UITextAlignmentCenter;
        [tCell.contentView addSubview:iName];
        [iName release];
        
        UILabel* iTime = [[UILabel alloc] initWithFrame:CGRectMake(108, 0,106, 40)];
        iTime.tag = 101;
        iTime.backgroundColor = [UIColor clearColor];
        [iTime setFont:[UIFont systemFontOfSize:15.0f]];
        [iTime setTextColor:[UIColor colorWithRed:19.0/255.0f green:19.0/255.0f blue:19.0/255.0f alpha:1.0f]];
        iTime.textAlignment = UITextAlignmentCenter;
        [tCell.contentView addSubview:iTime];
        [iTime release];
        
        UIButton* ibutton = [UIButton buttonWithType:UIButtonTypeCustom];
        ibutton.tag = 102;
        ibutton.frame = CGRectMake(230, 6, 75, 30);
        [ibutton setBackgroundImage:[UIImage imageNamed:@"button_yellow_tiny.png"] forState:UIControlStateNormal];
        [ibutton setBackgroundImage:[UIImage imageNamed:@"button_gray_tiny.png"] forState:UIControlStateHighlighted];
        [ibutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [ibutton setTitle:@"进 入" forState:UIControlStateNormal];
        [ibutton addTarget:self action:@selector(ChangeCellButton:) forControlEvents:UIControlEventTouchUpInside];
        [tCell.contentView addSubview:ibutton];
        ibutton = nil;
        
        UIImageView* tLineImgView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line.png"]];
        tLineImgView.frame =CGRectMake(0, 39, 320, 1);
        [tCell.contentView addSubview:tLineImgView];
        [tLineImgView release];
    }
    NSUInteger row = indexPath.row;
    NSDictionary* dic = nil;
    if (m_Goindextype == GREYOR_RS) {
        dic = [m_DataArraylist1 objectAtIndex:row];
    }
    else if (m_Goindextype == GREYOR_T) {
        dic = [m_DataArraylist2 objectAtIndex:row];

    }
    else if (m_Goindextype == GREYOR_GO) {
        dic = [m_DataArraylist3 objectAtIndex:row];
    }
    UILabel* ilabel = (UILabel*)[tCell viewWithTag:100];
    ilabel.text = (NSString*)[dic objectForKey:@"title"];
    UILabel* ilabel1 = (UILabel*)[tCell viewWithTag:101];
    ilabel1.text = (NSString*)[dic objectForKey:@"date"];
    UIButton* ibutton = (UIButton*)[tCell viewWithTag:102];
    ibutton.tag = row;

    return tCell;
}


#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    NSUInteger row = indexPath.row;
//    NSUInteger section = indexPath.section;

}

#pragma mark MBProgressHUDDelegate
- (void)hudWasHidden:(MBProgressHUD *)hud{
    m_MBHUD.delegate = nil;
	[m_MBHUD removeFromSuperview];
	[m_MBHUD release];
	m_MBHUD=nil;
}

-(void) addHudLoad{
	if (m_MBHUD == nil) {
		m_MBHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
		// Add HUD to screen
        AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
		[tAppDel.window addSubview:m_MBHUD];
		// Regisete for HUD callbacks so we can remove it from the window at the right time
		m_MBHUD.delegate = self;
		m_MBHUD.labelText = @"请求数据中...";
		[m_MBHUD show:YES];
	}
}
#pragma mark HttpInterfaceManageDelegate
-(void)RequestResultsGetGOSU:(NSDictionary*)dic
{
    if (dic == nil) {
        AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
        [tAppDel showWithCustomView:@"提示" detailText:@"数据异常" isCue:YES delayTime:2.0f];
    }else{
        [m_DataArraylist1 removeAllObjects];
        [m_DataArraylist2 removeAllObjects];
        [m_DataArraylist3 removeAllObjects];
        NSArray* iarray1 = [dic objectForKey:@"step1_list"];
        for (NSObject* object in iarray1) {
            [m_DataArraylist1 addObject:object];
        }
        NSArray* iarray2 = [dic objectForKey:@"step2_list"];
        for (NSObject* object in iarray2) {
            [m_DataArraylist2 addObject:object];
        }
        NSArray* iarray3  = [dic objectForKey:@"step3_list"];
        for (NSObject* object in iarray3) {
            [m_DataArraylist3 addObject:object];
        }
        [m_GoTableView reloadData];
    }
    [self hudWasHidden:nil];
}
-(void)RequestResultsErrer:(NSDictionary*)dic
{
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [tAppDel showWithCustomView:@"提示" detailText:[dic objectForKey:@"errorMsg"] isCue:YES delayTime:2.0f];
    [self hudWasHidden:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dealloc
{
    m_RSbutton = nil;
    m_Tbutton = nil;
    m_GObutton = nil;
    [m_label release];
    [m_label1 release];
    [m_label2 release];
    [m_titleBttBarItem release];
    [m_DataArraylist1 release];
    [m_DataArraylist2 release];
    [m_DataArraylist3 release];
    [m_GoTableView release];
    m_MBHUD = nil;
    [super dealloc];
}
@end
